using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using AjaxPro;

using CMS.Core.DataInterfaces;
using CMS.Data;
using CMS.Core.Domain;
using CMS.Presenters.Presenters;
using CMS.Presenters.Views;
using CMS.Web;
using ProjectBase.Utils;

namespace CMS.Web.ListManager
{
    public partial class EditDiaBan : BaseUserControl, IEditDiaBanView
    {
        private EditDiaBanPresenter presenter;
        private DiaBan obj;
        string MaDB = "";

        public EditDiaBan()
        {
            this.presenter = new EditDiaBanPresenter(this, NHibernateDaoFactory.Instance.GetDiaBanDao());
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            // For Ajax
            this.RaiseClientEvent();
            this.MaDB = CRequest.GetQueryUri("MaDiaBan", "0");
            presenter.InitViewForEditing(Convert.ToInt32(this.MaDB), this.IsPostBack);
            this.obj = presenter.getByID(Convert.ToInt32(this.MaDB));
            this.btnOK.Attributes.Add("onclick", "return CheckInput();");
        }

        // For Ajax to validdate
        protected void RaiseClientEvent()
        {
            Utility.RegisterTypeForAjax(typeof(CMS.Presenters.Presenters.EditLinhVucPresenter));
        }

        public string Message
        {
            set { //lblMessage.Text = value; 
            }
        }

        public DiaBan ObjToUpdate
        {
            get
            {
                if (this.obj != null)
                {
                    this.obj.TenDiaBan = txtName.Text.Trim();                    
                    if ((txtSTT.Text.Trim() != null) && (!txtSTT.Text.Trim().Equals("")))
                    {
                        obj.ThuTu = Convert.ToByte(txtSTT.Text.Trim());
                    }
                    this.obj.TrangThai = Convert.ToBoolean(radStatus.SelectedValue);
                }
                return this.obj;
            }
            set
            {
                if (value == null) throw new ArgumentNullException("ObjToUpdate may not be null");

                hidID.Value = value.ID.ToString();
                txtName.Text = value.TenDiaBan;                           
                txtSTT.Text = Convert.ToString(value.ThuTu);
              
                radStatus.SelectedValue = Convert.ToString(value.TrangThai);
                radStatus.SelectedIndex = (value.TrangThai)? 0 : 1;
            }
        }

        #region Button Members
        protected void btnOK_Click(object sender, EventArgs e)
        {
            presenter.UpdateObj(Page.IsValid);
            this.btnCancel_Click(this, null);
        }       

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(BaseUrl + "&action=VIEW&MaDiaBan=" + this.MaDB);
        }
        #endregion
    }
}